package com.ikas.ai.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

/**
 * 生成LOGO类.
 * @author HXH
 */
public class LogoUtil {

    private static Logger logger = LoggerFactory.getLogger(LogoUtil.class);

    /**
     * @param args 参数
     */
    public static void main(String[] args) {
        String title = "WEB通用平台";
        String fileName = PathUtil.getLibPath() + "test.png";
        genLogo(title, fileName);
        title = "Web Common Platform";
        fileName = PathUtil.getLibPath() + "test_en.png";
        genLogo(title, fileName);

        logger.debug("OK");
    }

    /**
     * 生成LOGO.
     * @param title
     * @param fileName
     * @throws IOException
     */
    public static void genLogo(String title, String fileName) {
        try {
            int width = 400;
            int height = 30;

            // 创建BufferedImage对象
            BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
            // 获取Graphics2D
            Graphics2D g2d = image.createGraphics();
            Font font = getFont();
            g2d.setFont(font);
            // 获得字符串的宽度和高度
            FontMetrics fm = g2d.getFontMetrics();
            width = fm.stringWidth(title) + 5;
            int fontHeight = fm.getHeight();
            // 释放对象
            g2d.dispose();

            image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
            g2d = image.createGraphics();
            // ---------- 增加下面的代码使得背景透明 -----------------
            image = g2d.getDeviceConfiguration().createCompatibleImage(width, height,
                    Transparency.TRANSLUCENT);
            // 释放对象
            g2d.dispose();
            g2d = image.createGraphics();
            // ---------- 背景透明代码结束 -----------------

            // 画图
            g2d.setFont(font);
            // 消除锯齿
            g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
                    RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
            int x = 0;
            int y = (int) ((height - fontHeight) * 1.25 + fontHeight);
            // 画阴影
            g2d.setColor(new Color(0xAA, 0xAA, 0xAA));
            g2d.drawString(title, x + 2, y + 3);
            // 边框
            g2d.setColor(Color.white);
            g2d.drawString(title, shiftWest(x, 1), shiftNorth(y, 1));
            g2d.drawString(title, shiftWest(x, 1), shiftSouth(y, 1));
            g2d.drawString(title, shiftEast(x, 1), shiftNorth(y, 1));
            g2d.drawString(title, shiftEast(x, 1), shiftSouth(y, 1));
            // 文字
            Color color = new Color(0x59, 0x57, 0x57);
            g2d.setColor(color);
            g2d.drawString(title, x, y);
            // 透明度设置 结束
            g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER));
            // 释放对象
            g2d.dispose();
            // 保存文件
            File imageFile = new File(fileName);
            String parentPath = imageFile.getPath();
            File pathFile = new File(parentPath);
            if (!pathFile.exists()) {
                pathFile.mkdirs();
            }
            ImageIO.write(image, "png", new File(fileName));
        } catch (Exception e) {
            logger.error("Generate logo failed.", e);
        }
    }

    private static int shiftWest(int value, int offset) {
        return value - offset;
    }

    private static int shiftEast(int value, int offset) {
        return value + offset;
    }

    private static int shiftNorth(int value, int offset) {
        return value - offset;
    }

    private static int shiftSouth(int value, int offset) {
        return value + offset;
    }

    /**
     * 加载指定字体.
     * @return
     */
    private static Font getFont() {
        Font font = null;
        try {
            String path = String.format("%sfiles/msyhbd.ttc", PathUtil.getBinOrJarPath());
            File file = new File(path);
            if (file.exists()) {
                font = Font.createFont(Font.TRUETYPE_FONT, file);
                font = font.deriveFont(Font.BOLD, 30);
            } else {
                logger.error("Font file not exits.");
            }
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return font;
    }
}
